<?php

/*
 * @类名:   page
 * @参数:   $myde_total - 总记录数
 *          $myde_size - 一页显示的记录数
 *          $myde_page - 当前页
 *          $myde_url - 获取当前的url
 */

class Page {

    private $myde_total;          //总记录数
    private $myde_size;           //一页显示的记录数
    private $myde_page;           //当前页
    private $myde_page_count;     //总页数
    private $myde_i;              //起头页数
    private $myde_en;             //结尾页数
    private $myde_url;            //获取当前的url
    private $where;               //分页搜索条件

    /*
     * $show_pages
     * 如$show_pages=2那么页面上显示就是[首页] [上页] 1 2 3 4 5 [下页] [尾页]
     */
    private $show_pages;

    /**
     * @param int $myde_total  总页数
     * @param int $myde_size   每页数量
     * @param int $myde_page   当前是第几页
     * @param $myde_url        跳转连接
     * @param int $show_pages  显示样式如$show_pages=2那么页面上显示就是[首页] [上页] 1 2 3 4 5 [下页] [尾页]
     * @param string $search
     * @param string $key
     */
    public function __construct($myde_total=1, $myde_size = 1, $myde_page = 1, $myde_url, $show_pages = 2,$search='',$key='') {
        $this->myde_total      = $myde_total;//$this->numeric($myde_total);
        $this->myde_size       = $this->numeric($myde_size);
        $this->myde_page       = $this->numeric($myde_page);
        $this->myde_page_count = ceil($this->myde_total / $this->myde_size);//总页数
        $this->myde_url        = $myde_url;  //  /Order/index
        $this->search          = $search;
        $this->key             = $key;
        if ($this->myde_total < 0){
            $this->myde_total = 0;
        }
        if ($this->myde_page < 1){
            $this->myde_page = 1;
        }

        if ($this->myde_page_count < 1){
            $this->myde_page_count = 1;
        }

        if ($this->myde_page > $this->myde_page_count){
            $this->myde_page = $this->myde_page_count;
        }

        $this->limit = ($this->myde_page - 1) * $this->myde_size;
        $this->myde_i = $this->myde_page - $show_pages;
        $this->myde_en = $this->myde_page + $show_pages;
        if ($this->myde_i < 1) {
            $this->myde_en = $this->myde_en + (1 - $this->myde_i);
            $this->myde_i = 1;
        }
        if ($this->myde_en > $this->myde_page_count) {
            $this->myde_i = $this->myde_i - ($this->myde_en - $this->myde_page_count);
            $this->myde_en = $this->myde_page_count;
        }
        if ($this->myde_i < 1){
            $this->myde_i = 1;
        }
    }

    //检测是否为数字
    private function numeric($num) {
        if (strlen($num)) {
            if (!preg_match("/^[0-9]+$/", $num)) {
                $num = 1;
            } else {
                $num = substr($num, 0, 11);
            }
        } else {
            $num = 1;
        }
        return $num;
    }

    //地址替换
    /*private function page_replace($page) {
        return str_replace("{page}", $page, $this->myde_url);
    }*/
    private function page_url($page){
        if($this->search && $this->key){
            if (strpos($this->myde_url,'?')) {
               $url =  $this->myde_url.'&p='.$page.'&search='.$this->search.'&key='.$this->key.'&pageSize='.$this->myde_size;
            } else {
                $url =   $this->myde_url.'?p='.$page.'&search='.$this->search.'&key='.$this->key.'&pageSize='.$this->myde_size;
            }
            return $url;
        }else{
            if (strpos($this->myde_url,'?')) {
                $url =  $this->myde_url.'&p='.$page.'&pageSize='.$this->myde_size;
            } else {
                $url =   $this->myde_url.'?p='.$page.'&pageSize='.$this->myde_size;;
            }
            return $url;
        }
    }

    //数据总条数
    private function total()
    {
        return '<li style="float: left;line-height: 2.42;margin-right: 1em;">共'.$this->myde_total.'条数据</i></li>';
    }

    //数据每显示下拉列表框
    private function page_size()
    {
        $pageSizeList = self::PageSizeArray();
        $html = '<li style="float: left;line-height: 2.42;margin-right: 1em;">每页显示<select id="page_size" name="page_size" onchange="changePageSize()">';
        foreach($pageSizeList as $key => $value) {
            if( $this->myde_size == $value) {
                $html.='<option value="'.$key.'" selected>'.$value.'</option>';
            } else {
                $html.='<option value="'.$key.'">'.$value.'</option>';
            }

        }
        $html .='</select>条</li>';
        return $html;
    }

    //首页
    private function home() {
        if ($this->myde_page != 1) {
            return "<li><a class='page-button' href='" . $this->page_url(1) . "'>首页</a></li>";
        } else {
            return "<li class='disabled'><a href='javascript:void(0)'>首页</a></li>";
        }
    }

    //上一页
    private function prev() {
        if ($this->myde_page != 1) {
            return "<li><a class='page-button' href='" . $this->page_url($this->myde_page - 1) . "'><i class='fa fa-chevron-left'></i></a></li>";
        } else {
            return "<li><a href='javascript:void(0)'><i class='fa fa-chevron-left'></i></a></li>";
        }
    }

    //下一页
    private function next() {
        if ($this->myde_page != $this->myde_page_count) {
            return "<li><a class='page-button' href='" . $this->page_url($this->myde_page + 1) . "'><i class='fa fa-chevron-right'></i></a></li>";
        } else {
            return"<li><a href='javascript:void(0)'><i class='fa fa-chevron-right'></i></a></li>";
        }
    }

    //尾页
    private function last() {
        if ($this->myde_page != $this->myde_page_count) {
            return "<li><a class='page-button' href='" . $this->page_url($this->myde_page_count) . "'>尾页</a></li>";
        } else {
            return "<li class='disabled'><a href='javascript:void(0)'>尾页</a></li>";
        }
    }

    //输出
    public function showPage() {
        $str = "<ul class='pagination pull-right'>";
        $str.=$this->total();
        $str.=$this->page_size();
        $str.=$this->home();
        $str.=$this->prev();
        if ($this->myde_i > 1) {
            $str.="<li><a href='javascript:void(0)'>...</a></li>";
        }
        for ($i = $this->myde_i; $i <= $this->myde_en; $i++) {
            if ($i == $this->myde_page) {
                $str.="<li class='active'><a class='page-button' href='" . $this->page_url($i) . "'>".$i."</a></li>";
            } else {
                $str.="<li><a class='page-button' href='" . $this->page_url($i) . "'>".$i."</a></li>";
            }
        }
        if ($this->myde_en < $this->myde_page_count) {
            $str.="<li><a href='javascript:void(0)'>...</a></li>";
        }
        $str.=$this->next();
        $str.=$this->last();
        $str.="</ul>";
        return $str;
    }

    public static function ValidatePageSize($size, $myde_url)
    {
        if (!empty($size) && !in_array($size, self::PageSizeArray())) {
            $size = 10;
        }
        $sessionPage = new Zend_Session_Namespace('Page');
        $sessionName = 'pageSize:' . str_replace('/', '_', $myde_url);
        if (isset($sessionPage->$sessionName) && ($size == $sessionPage->$sessionName || empty($size))) {
            $size = $sessionPage->$sessionName;
        } else if (!empty($size)) {
            $sessionPage->$sessionName = $size;
            $sessionPage->setExpirationSeconds(3153600000, $sessionName); //永久有效 100年
        } else {
            $size = 10;
        }
        return $size;
    }

    private static function PageSizeArray()
    {
        return array(
            '10' => 10,
            '20' => 20,
            '50' => 50,
            '100' => 100,
        );
    }
}

?>